# target
TARGET=nano

BUILD_DIR=build

CPU =cortex-m33
# gcc
CROSS_COMPILE=arm-none-eabi-
CC=$(CROSS_COMPILE)gcc
AS=$(CROSS_COMPILE)gcc -x assembler-with-cpp
CP=$(CROSS_COMPILE)objcopy
SZ=$(CROSS_COMPILE)size
LD=$(CROSS_COMPILE)ld
DP=$(CROSS_COMPILE)objdump

# SRC 
SRC_C=src/main.c src/interrupt.c src/led.c src/gcc_startup.c src/resets.c src/gpio.c src/sysclk.c 
SRC_C+=src/syscalls.c src/sysmem.c

#SRC_S=src/bs2_default_padded_checksummed.S
SRC_S=src/image_def_block.S
SRC_LD=link.ld

OBJC=$(addprefix $(BUILD_DIR)/,$(notdir $(SRC_C:.c=.o)))
OBJS=$(addprefix $(BUILD_DIR)/,$(notdir $(SRC_S:.S=.o)))

INC=-Isrc -I. -Iinc
 
#CFLAGS
CFLAGS= \
-mcpu=$(CPU) \
-mthumb \
-mfloat-abi=soft \
-Wall \
-fdata-sections -ffunction-sections \
-g -g3 -O0

CFLAGS+= \
--specs=nano.specs 

LIBS=\
#-lc -lm
#如果程序并不实际使用系统函数，但是某些代码引用了系统函数，那么可以引入libnosys，以便通过编译。
#LIBS+=-lnosys

# LD FLAGS
LDFLAGS=-mcpu=$(CPU) -mthumb \
-Wl,--gc-sections \
-Wl,--start-group -lc -lm -Wl,--end-group \
--specs=nano.specs

LDFLAGS+=-T$(SRC_LD) $(LIBS)

LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
 

#SET PATH %.c %.S
vpath %.c $(sort $(dir $(SRC_C)))
vpath %.S $(sort $(dir $(SRC_S)))

ELF=$(BUILD_DIR)/$(TARGET).elf
BIN=$(BUILD_DIR)/$(TARGET).bin
DIS=$(BUILD_DIR)/$(TARGET).dis
UF2=$(BUILD_DIR)/$(TARGET).uf2

.PHONY : all clean d c download download2 download3

all: $(BUILD_DIR) $(BIN) $(DIS) 

$(BUILD_DIR):
	mkdir $@

# cc .c
$(BUILD_DIR)/%.o:%.c 
	@echo .CC $<
	@$(CC) $(CFLAGS) -c $< $(INC) -o $@

# as .S
$(BUILD_DIR)/%.o:%.S 
	@echo .AS $<
	@$(AS) $(CFLAGS) -c $< $(INC) -o $@

# elf
$(ELF):$(BUILD_DIR) $(OBJC) $(OBJS)
	@echo link elf $@
	$(CC) $(OBJC) $(OBJS) $(LDFLAGS)  -o $@
	@$(SZ) $@ 
#	../../tools/elf2uf2 $(ELF) $(UF2)
	../../tools/picotool uf2 convert  --verbose  $(ELF)  $(UF2) 
#--abs-block 

#cp .bin
$(BIN):$(ELF)
	@echo objcopy bin
	$(CP) -O binary -S $< $@


$(DIS):$(ELF)
	@echo objdump dis
	@$(DP) -D -h -s $< > $@

clean:
	rm -rf $(BUILD_DIR)

d:
	make clean
	make all

c:$(BUILD_DIR) $(OBJC) $(OBJS)
	@echo .o

download2:
#  openocd 下载 -s 指定默认目录 -f 都在此目录下工作可以省略前缀
#  program 编程 
	../../tools/xpack-openocd-0.12.0-6/bin/openocd.exe \
	-s ../../tools/xpack-openocd-0.12.0-6/openocd/scripts \
	-f interface/cmsis-dap.cfg \
	-f target/rp2350.cfg \
	-c "program build/nano.elf verify reset" \
	-c "reset run" \
	-c exit

download1:
	C:/Users/purple/.pico-sdk/openocd/0.12.0+dev/openocd.exe \
	-s C:/Users/purple/.pico-sdk/openocd/0.12.0+dev/scripts \
	-f interface/cmsis-dap.cfg \
	-f target/rp2350.cfg \
	-c "adapter speed 100" \
	-c "program build/nano.elf verify reset" \
	-c "reset run" \
	-c exit

download:
	../../tools/openocd-rpxx/openocd.exe \
	-s ../../tools/openocd-rpxx/scripts \
	-f interface/cmsis-dap.cfg \
	-f target/rp2350.cfg \
	-c "adapter speed 1000" \
	-c "program build/nano.elf verify reset" \
	-c "reset run" \
	-c exit

#define PROBE_PIN_OFFSET 12
#define PROBE_PIN_SWCLK (PROBE_PIN_OFFSET + 0) //12 swclk 
#define PROBE_PIN_SWDI (PROBE_PIN_OFFSET + 1)  //13 swdi
#define PROBE_PIN_SWDIO (PROBE_PIN_OFFSET + 2) //14 swdio
download3:
	../../tools/openocdmy/openocd.exe \
	-s ../../tools/openocdmy/scripts \
	-f interface/cmsis-dap.cfg \
	-f target/rp2350.cfg \
	-c "adapter speed 100" \
	-c "program build/nano.elf verify reset" \
	-c "reset run" \
	-c exit

 